# -*- coding: utf-8 -*-
from django.utils.html import strip_tags

def get_keywords( desc):
    exclusion = ( "que", "las", "muy","con",u"más","mas",
                  "este","esta","esto","desde", "para", 
                  "una", "uno", "unas" ,"unos", "han")
#    desc = strip_tags(unicode(desc))
    for word in exclusion:
        desc = desc.replace (word, "")
    desc = desc.strip(".,:;")
    words = desc.split()
    word_freq = {}
    for word in words:
        if len(word) > 2:
            if word in word_freq:
                word_freq[word] += 1
            else:
                word_freq[word] = 1
        else:
            words.remove(word)
    word_prob_dict = {}
    size_corpus = len(words)
    for word in word_freq:
        word_prob_dict[word] = float(word_freq[word])/size_corpus

    prob_list = []
    for word, prob in word_prob_dict.items():
         prob_list.append(prob)
    non_exist_prob = min(prob_list)/2

    test_word_freq = {}
    for word in words:
        if len(word) > 2:
            if word in test_word_freq:
                test_word_freq[word] += 1
            else:
                test_word_freq[word] = 1
        else:
            words.remove(word)
    
    test_word_ba_list = []
    for word, ba in test_word_freq.items():
         test_word_ba_list.append((word, ba))

    def sort_func(a, b):
        if a[1] > b[1]:
           return -1
        elif a[1] < b[1]:
            return 1
        return 0

    test_word_ba_list.sort(sort_func)

    keywords_str = ""
    i = 0
    for keyword in test_word_ba_list:
        if (i < 10):
            keywords_str = keywords_str + keyword[0] + ", "
        else:
            return keywords_str
        i += 1
    return keywords_str